﻿#include("../../layout/header.html")
#@layout()
#define content()
<head>
    <style>
        /* 左树 */
        #organizationTreeBar {
            padding: 10px 15px;
            border: 1px solid #e6e6e6;
            background-color: #f2f2f2;
        }

        #organizationTree {
            border: 1px solid #e6e6e6;
            border-top: none;
            padding: 10px 5px;
            overflow: auto;
            height: -webkit-calc(100vh - 125px);
            height: -moz-calc(100vh - 125px);
            height: calc(100vh - 125px);
        }

        .layui-tree-entry .layui-tree-txt {
            padding: 0 5px;
            border: 1px transparent solid;
            text-decoration: none !important;

        }

        .layui-tree-entry.ew-tree-click .layui-tree-txt {
            background-color: #fff3e0;
            border: 1px #FFE6B0 solid;
        }

        /* 右表搜索表单 */
        #organizationUserTbSearchForm .layui-form-label {
            box-sizing: border-box !important;
            width: 90px !important;
        }

        #organizationUserTbSearchForm .layui-input-block {
            margin-left: 90px !important;
        }
    </style>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid" style="padding-bottom: 0;">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 10px;">
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键字:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入组织架构名称"/>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">类别:</label>
                        <div class="layui-input-inline">
                            <select name="type" id="typeSelect" class="layui-select"></select>
                        </div>
                    </div>

                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="orgTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                    </div>
                </div>
            </form>


            <!-- 树工具栏 -->
            <div class="layui-form toolbar" id="organizationTreeBar">
                <button id="organizationAddBtn" class="layui-btn layui-btn-sm icon-btn">
                    <i class="layui-icon">&#xe654;</i>添加
                </button>&nbsp;
                <!--<button id="organizationEditBtn" class="layui-btn layui-btn-sm layui-btn-warm icon-btn">
                    <i class="layui-icon">&#xe642;</i>修改
                </button>&nbsp;
                <button id="organizationDelBtn"
                        class="layui-btn layui-btn-sm layui-btn-danger icon-btn">
                    <i class="layui-icon">&#xe640;</i>删除
                </button>-->
            </div>
            <!-- 左树 -->
            <table id="organizationTree"></table>
        </div>
    </div>
</div>

<!-- 表单弹窗1 -->
<script type="text/html" id="organizationEditDialog">
    <form id="organizationEditForm" lay-filter="organizationEditForm" class="layui-form model-form"
          style="padding-right: 20px;">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">上级机构:</label>
            <div class="layui-input-block">
                <div id="pid" class="ew-xmselect-tree"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">机构名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入机构名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">机构类型:</label>
            <div class="layui-input-block">
                <select id="orgType" name="type" lay-verType="tips" lay-verify="required" required>

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">排序号:</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序号" class="layui-input" type="number" value="1"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remarks" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn" lay-filter="organizationEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="organizationUserTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'tree', 'dropdown', 'xmSelect', 'treeTable', 'formX','badgeX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tree = layui.tree;
        var xmSelect = layui.xmSelect;
        var formX = layui.formX;
        var badgeX = layui.badgeX;
        var treeTable = layui.treeTable;
        var selObj, treeData, insTb;  // 左树选中数据
        var orgType = [];

        treeRender();

        function treeRender(param) {
            admin.req('/sys/sysOrg/list',param ? param : {page: 1, limit: 10000}, function (d) {
                if (d.code === 0) {
                    insTb = treeTable.render({
                        elem: '#organizationTree',
                        data: d.data,
                        tree: {
                            iconIndex: 2,           // 折叠图标显示在第几列
                            isPidData: true,        // 是否是id、pid形式数据
                            idName: 'id',  // id字段名称
                            pidName: 'pid'     // pid字段名称
                        },
                        size: 'sm',
                        height: 'full-150',
                        cols: [[
                            {type: "checkbox", fixed: "left", width: 50},
                            {type: "numbers", title: '序号', width: 60},
                            {field: 'name', title: '部门名称', minWidth: 260},
                            {field: 'fullName', title: '部门全称', minWidth: 260,templet:function (d) {
                                    return '<a style="line-height: 22px;font-size: 14px;color: #1e9fff;">'+d.fullName+'</a>';
                                }},
                            {field: 'typeStr', title: '类别', minWidth: 60,templet: function (d) {
                                    let res,type = d.type,typeStr = d.typeStr;
                                    if(type ===0 ){
                                        res = badgeX.initBlue(typeStr,'');
                                    }else if(type === 1){
                                        res = badgeX.initGreen(typeStr,'');
                                    }else if(type === 2){
                                        res = badgeX.initYellow(typeStr,'');
                                    }else if(type === 3){
                                        res = badgeX.initBlack(typeStr,'');
                                    }else if(type === 4){
                                        res = badgeX.initGray(typeStr,'');
                                    }
                                    return res;
                                }},
                            {field: 'sort', title: '排序', edit: 'text'},
                            {field: 'createBy', title: '创建人'},
                            {
                                field: 'createTime', title: '创建时间'
                            },
                            {
                                title: '操作',
                                minWidth: 150,
                                templet: '#organizationUserTbBar',
                                fixed: "right",
                                align: "center"
                            }
                        ]]
                        , done: function (d) {
                            // insTb.expandAll();
                            treeData = d;
                        }
                    });
                    insTb.expandAll();


                    /* 表格工具条点击事件 */
                    treeTable.on('tool(organizationTree)', function (obj) {
                        if (obj.event === 'edit') { // 修改
                            showEditModel(obj.data);
                        } else if (obj.event === 'del') { // 删除
                            doDel(obj);
                        }
                    });

                    /* 表格搜索 */
                    form.on('submit(orgTbSearch)', function (data) {
                        data.field.page = 1;
                        data.field.limit = 10000;
                        console.log(data.field)
                        treeRender(data.field);
                        return false;
                    });

                    /* 添加 */
                    $('#organizationAddBtn').click(function () {
                        showEditModel(null, selObj ? selObj.data.pid : null);
                    });

                    /* 修改 */
                    $('#organizationEditBtn').click(function () {
                        if (!selObj) return layer.msg('未选择机构', {icon: 2});
                        showEditModel(selObj.data);
                    });

                    /* 删除 */
                    $('#organizationDelBtn').click(function () {
                        if (!selObj) return layer.msg('未选择机构', {icon: 2});
                        doDel(selObj);
                    });
                }
            })
        }

        admin.req('/sys/sysDictData/get', {type: 'orgType'}, function (d) {
            if (d.code === 0) {
                orgType = d.data.orgType;

                formX.renderSelect({
                    elem: '#typeSelect',
                    data: orgType,
                    name: 'name',
                    value: 'value',
                    hint: '请选择数据类别'
                });
            }
        })

        /* 显示表单弹窗 */
        function showEditModel(mData, pid) {
            admin.popupRight({
                type: 1,
                // area: '600px',
                title: (mData ? '修改' : '添加') + '机构',
                content: $('#organizationEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('organizationEditForm', mData);
                    formX.renderSelect({
                        elem: '#orgType',
                        data: orgType,
                        name: 'name',
                        value: 'value',
                        initValue: mData ? mData.type : "",
                        hint: '请选择数据类别'
                    });

                    // 表单提交事件
                    form.on('submit(organizationEditSubmit)', function (data) {
                        data.field.pid = insXmSel.getValue('valueStr');
                        var loadIndex = layer.load(2);
                        admin.post('/sys/sysOrg/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });
                    // 渲染下拉树
                    var insXmSel = xmSelect.render({
                        el: '#pid',
                        height: '250px',
                        data: treeData,
                        initValue: mData ? [mData.pid] : (pid ? [pid] : []),
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'name',
                            value: 'id'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: true
                        }
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除此机构吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/sysOrg/del', {
                    id: obj.data.id,
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        treeRender()
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }


    });
</script>
</body>
#end
